﻿using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MyMedia.iPod.Music;
using MyMedia;
using System.Drawing;
using System.Runtime.InteropServices;

namespace MyMedia_App.Pages
{
    public partial class iPod_Tab : Form
    {
        MyMedia.iPod.Music.ITunesDB DB = new ITunesDB();
        MyMedia.iPod.iPod iPod = new MyMedia.iPod.iPod();
        MyMedia.MediaPlayer MP = new MediaPlayer();
        Timer SongTick = new Timer();

        public iPod_Tab()
        {
            InitializeComponent();
            PlayingTitle.Text = string.Empty;
            volumeControl1.slider.ValueChanged += new System.Windows.RoutedPropertyChangedEventHandler<double>(slider_ValueChanged);
            volumeControl1.slider.Value = 500;

            SongTick.Interval = 1;
            SongTick.Tick += new EventHandler(SongTick_Tick);
        }
        private void iPod_Tab_Load(object sender, EventArgs e)
        {
            if (iPod.FindiPod() != string.Empty)
            {
                try
                {
                    DB = new MyMedia.iPod.Music.ITunesDB();
                    DB.Load(iPod.FindiPod() + "\\iPod_Control\\iTunes\\iTunesDB");
                    TrackItem[] songs = DB.GetSongs();
                    // TrackNumbers is an absolute must for speed when playing a song with the most accurate info for the song
                    int TrackItemNumbers = 0;
                    foreach (TrackItem item in songs)
                    {
                        TrackItemNumbers++;
                        ListViewItem item2 = new ListViewItem();
                        // No support for .m4a & .aac in the MediaPlayer API 
                        if (Path.GetExtension(item.Location) == ".m4a" && Path.GetExtension(item.Location) == ".aac")
                        {
                            item2.BackColor = Color.Red;
                        }
                        item2.Text = item.Title.StartsWith(" ") ? item.Title.TrimStart(new char[] { ' ', '/' }) : item.Title;
                        string Artist = (item.Artist != null) ? item.Artist : "Unknown Artist";
                        string Album = (item.Album != null) ? item.Album : "Unknown Album";
                        string Genre = (item.Genre != null) ? item.Genre : "Unknown Genre";
                        item2.SubItems.Add(Artist);
                        item2.SubItems.Add(Album);
                        item2.SubItems.Add(Genre);
                        item2.SubItems.Add(MillisecondsToTime(item.Length));
                        item2.Tag = TrackItemNumbers.ToString();
                        this.SongList.Items.Add(item2);
                    }
                    SongList.Sorting = SortOrder.Ascending;
                }
                catch
                {
                    PlayingTitle.Text = "Error Loading iTunesDB";
                }
            }
        }
        private void SongList_DoubleClick(object sender, EventArgs e)
        {
            if (SongList.SelectedItems.Count > 0)
            {
                TrackItem[] SongItem = DB.GetSongs();
                // Because of a little bug - possibly because of me - we have to subtract 1 to get the correct TrackItem[] info
                int SongNumber = Convert.ToInt32(SongList.SelectedItems[0].Tag) - 1;
                if (MP.Open(iPod.FindiPod() + "\\" + SongItem[SongNumber].Location))
                {
                    if (MP.Play())
                    {
                        PlayingTitle.Text = SongItem[SongNumber].Title;
                        button2.Enabled = true;
                        button3.Enabled = true;
                        button2.Text = "Pause";
                        timeControl2.Duration = SongItem[SongNumber].Length;
                        MP.SetVolume(Convert.ToInt32(volumeControl1.slider.Value));
                        SongTick.Start();
                    }
                    else
                    {
                        PlayingTitle.Text = "Error Playing Song!";
                    }
                }
                else
                {
                    PlayingTitle.Text = "Error Opening Song!";
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "Pause")
            {
                button2.Text = "Play";
                MP.Pause();
                SongTick.Stop();
            }
            else if (button2.Text == "Play")
            {
                button2.Text = "Pause";
                MP.Play();
                SongTick.Start();
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
                MP.Stop();
                SongTick.Stop();
                MP.Close();
                button2.Enabled = false;
                button3.Enabled = false;
                PlayingTitle.Text = string.Empty;
        }
        public void slider_ValueChanged(object sender, EventArgs e)
        {
            MP.SetVolume(Convert.ToInt32(volumeControl1.slider.Value));
        }
        public void SongTick_Tick(object sender, EventArgs e)
        {
            timeControl2.PlayerPosition = Convert.ToInt32(MP.Duration()) - 1;
        }
        public string MillisecondsToTime(int Milliseconds)
        {
            TimeSpan t = TimeSpan.FromMilliseconds(Milliseconds);
            return string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds);
        }
    }
}
